﻿using AspNetCoreRateLimit;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using System.Text.Json;

namespace MyCompany.MyProject.WebApi
{
    public class CustomIpPolicyStore : IIpPolicyStore
    {
        private readonly IDistributedCache _cache;
        private readonly IpRateLimitPolicies _policies;

        public CustomIpPolicyStore(IDistributedCache cache, IOptions<IpRateLimitPolicies> policies)
        {
            _cache = cache;
            _policies = policies.Value;
        }

        public Task SetAsync(string id, IpRateLimitPolicies entry, TimeSpan? expirationTime = null, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public async Task<IpRateLimitPolicies> GetAsync(string id, CancellationToken cancellationToken = default)
        {
            var policies = await _cache.GetStringAsync("ip_rate_limit_policies", cancellationToken);
            if (!string.IsNullOrEmpty(policies))
            {
                return JsonSerializer.Deserialize<IpRateLimitPolicies>(policies);
            }

            return _policies;
        }

        public Task RemoveAsync(string id, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public Task SeedAsync()
        {
            throw new NotImplementedException();
        }

        public Task<bool> ExistsAsync(string id, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }
    }
}
